package com.dianning.web.action;

import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.*;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.SessionAware;

import com.dianning.bean.Company;
import com.dianning.bean.Money;
import com.dianning.bean.Profile;
import com.dianning.bean.Resume;
import com.dianning.bean.UserDetail;
import com.dianning.bean.UserInfo;
import com.dianning.bean.Visitor;
import com.dianning.comm.Constant;
import com.dianning.comm.DianningUtil;
import com.dianning.comm.Util;
import com.dianning.exception.DianningException;
import com.dianning.service.UserInfoService;
import com.dianning.service.VisitorService;
import com.dianning.web.interceptor.RemoteAddrAware;
import com.dianning.web.servlet.UserCookie;
import com.dianning.web.servlet.UserSession;

public class Login extends BaseAction implements SessionAware, RemoteAddrAware{

	private static final Log logger = LogFactory.getLog(Login.class);

	private UserInfoService userInfoService;

	public UserInfoService getUserInfoService() {
		return userInfoService;
	}

	public void setUserInfoService(UserInfoService userInfoService) {
		this.userInfoService = userInfoService;
	}
	
	private String remoteAddr;
	private Map session;
	
	public String getRemoteAddr() {
		return remoteAddr;
	}

	public void setRemoteAddr(String remoteAddr) {
		this.remoteAddr = remoteAddr;
	}
	
	public Map getSession() {
		return session;
	}
	
	public void setSession(Map session) {
		this.session = session;
	}	
	
	private String username;
	private String password;
	private boolean auto = false;
	private String tourl;

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public boolean isAuto() {
		return auto;
	}

	public void setAuto(boolean auto) {
		this.auto = auto;
	}

	public String getTourl() {
		return tourl;
	}

	public void setTourl(String tourl) {
		this.tourl = tourl;
	}

	public String execute() {
		try {
			return this.executeMethod(this.getAction());
		} catch (Exception e) {
			logger.error(e);
			return ERROR;
		}
	}

	public String index(){
		this.setAction("login");
        return INPUT;
	}
	
	public String popup(){
		this.setAction("poplogin");
        return "popLogin";
	}
	
	public String login(){
		//查找管理员
		UserInfo ui = this.getUserInfoService().findUserInfoByUsername(this.username);
		if(ui == null) {
			this.addActionError(this.getText("error.login.user"));
			this.getSession().put("code", "user_error");
			return INPUT;
		}
		//判断密码
		if(ui.getAvailable()!=1){
			this.addActionError(this.getText("error.login.available"));
			this.getSession().put("code", "available_error");
			return INPUT;
		}
		//判断密码
		if(!this.password.equalsIgnoreCase(ui.getPassword())){
			this.addActionError(this.getText("error.login.password"));
			this.getSession().put("code", "password_error");
			return INPUT;
		}
		
		if(ui.getAvailable()==2){
			this.addActionError(this.getText("error.login.user"));
			this.getSession().put("code", "user_error");
			return INPUT;
		}
		/**
		if (hasActionErrors()||hasFieldErrors()) {	
	        return INPUT;
        }
        */
		ui.setLoginTimes(ui.getLoginTimes() + 1);
		ui.setLastLoginIP(getRemoteAddr());
		ui.setLastLoginTime(new Date());
		this.getUserInfoService().updateUserInfo(ui);
		
		UserSession us = new UserSession();
		us.setUid(ui.getId());
		us.setUsername(ui.getUsername());
		us.setName(ui.getName());
		this.getSession().put(Constant.USER_SESSION_KEY, us);
		//自动登陆
		if(this.auto){
			HttpServletRequest request = ServletActionContext.getRequest();
	        HttpServletResponse response = ServletActionContext.getResponse(); 
			UserCookie uc = new UserCookie(request, response);
			String auth = DianningUtil.encodeAuth(ui.getId());
			uc.addCookie(Constant.COOKIE_AUTOLOGIN_AUTH, auth, 31536000);
		}
		return SUCCESS;
	}
	
	public String poplogin(){
		//查找管理员
		UserInfo ui = this.getUserInfoService().findUserInfoByUsername(this.username);
		if(ui == null) {
			this.addActionError(this.getText("error.login.user"));
			this.getSession().put("code", "user_error");
			return "popLogin";
		}
		//判断密码
		if(ui.getAvailable()!=1){
			this.addActionError(this.getText("error.login.available"));
			this.getSession().put("code", "available_error");
			return "popLogin";
		}
		//判断密码
		if(!this.password.equalsIgnoreCase(ui.getPassword())){
			this.addActionError(this.getText("error.login.password"));
			this.getSession().put("code", "password_error");
			return "popLogin";
		}
		/**
		if (hasActionErrors()||hasFieldErrors()) {	
	        return INPUT;
        }
        */
		ui.setLoginTimes(ui.getLoginTimes() + 1);
		ui.setLastLoginIP(getRemoteAddr());
		ui.setLastLoginTime(new Date());
		this.getUserInfoService().updateUserInfo(ui);
		
		UserSession us = new UserSession();
		us.setUid(ui.getId());
		us.setUsername(ui.getUsername());
		us.setName(ui.getName());
		this.getSession().put(Constant.USER_SESSION_KEY, us);
		return SUCCESS;
	}

}
